package com.sky.demo.activities;

import java.util.Calendar;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SlidingPaneLayout;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.sky.demo.R;
import com.sky.demo.global.BaseActivity;
import com.sky.demo.utils.ToastUtils;
import com.sky.demo.views.SolarSystem;
/**
 * @author 彬 QQ 1136096189
 * @Description: 首界面，卫星式菜单
 * @date 2015/8/17 15:30
 */
public class MainActivity extends BaseActivity {
    //	PushNotification(R.drawable.ic_launcher, "测试", "内容测试", MainActivity.class,
//					 "嘻嘻");
    private AnimationDrawable animationDrawable;
    Handler handler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 101)
                animationDrawable.stop();
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative);
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int screenWidth = outMetrics.widthPixels;
        LayoutParams lp = relative.getLayoutParams();
        lp.width = screenWidth / 3;
        lp.height = screenWidth / 3;
        relative.setLayoutParams(lp);
        relative.setBackgroundResource(R.drawable.animation_rect_list);
        animationDrawable = (AnimationDrawable) relative.getBackground();
        SolarSystem solarSystem = (SolarSystem) findViewById(R.id.solar);
        int childCount = solarSystem.getChildCount();
        FrameLayout.LayoutParams tvParams = new FrameLayout.LayoutParams(
                screenWidth / 5, screenWidth / 5);
        //
        for (int i = 0; i < childCount - 1; i++) {
            solarSystem.getChildAt(i).setLayoutParams(tvParams);
        }
        solarSystem.setRadius(screenWidth / 3);
        solarSystem.setRotaMenu(true);
        solarSystem.setOnMenuState(new SolarSystem.MenuState() {
            @Override
            public void openMenu() {
                animationDrawable.start();
                handler.sendEmptyMessageDelayed(101, 600);
            }

            @Override
            public void closeMenu() {
                animationDrawable.start();
                handler.sendEmptyMessageDelayed(101, 600);
            }
        });
        solarSystem.setOnMenuItemClickListener(new SolarSystem.onMenuItemClickListener() {

            @Override
            public void onItem(View view, int position) {
                String tag = (String) view.getTag();
                if (tag.equals("flow")) {
                    startActivity(new Intent(MainActivity.this, FlowActivity.class));
                } else if (tag.equals("list")) {
                    startActivity(new Intent(MainActivity.this, RefreshListActivity.class));
                } else if (tag.equals("viewpager")) {
                    startActivity(new Intent(MainActivity.this, ViewPagerActivity.class));
                } else if (tag.equals("menu_01")) {
                    startActivity(new Intent(MainActivity.this, RecyclerActivity.class));
                } else if (tag.equals("menu_02")) {
                    startActivity(new Intent(MainActivity.this, ZoomImageActivity.class));
                } else if (tag.equals("menu_03")) {
                    startActivity(new Intent(MainActivity.this, SlidingMenuActivity.class));

                }

                Calendar c = Calendar.getInstance();
                int day = c.get(Calendar.DAY_OF_MONTH);
            }
        });
    }
    private long lastBack;
    @Override
    public void onBackPressed() {
        long cm = System.currentTimeMillis();
        if (cm - lastBack > 3000) {
            ToastUtils.showShort(this,getResources().getString(R.string.exit));
            lastBack = cm;
        } else {
            super.onBackPressed();
        }
    }
}
